﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyConsoleApp.Code
{
    /*统计年龄大于22的，各个年龄都有谁
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>();
            list.Add(new Person { ID = 1, Name = "user1", Age = 23 });
            list.Add(new Person { ID = 2, Name = "user2", Age = 24 });
            list.Add(new Person { ID = 3, Name = "user3", Age = 23 });
            list.Add(new Person { ID = 4, Name = "user4", Age = 25 });
            list.Add(new Person { ID = 5, Name = "user5", Age = 20 });

            var result = list.MapReduce<Person, int, string, string>(
                Map,
                (key, values) => string.Join(",", values)
                );

            foreach (var d in result)
            {
                Console.WriteLine(d.Key + ":" + d.Value);
            }

        }

        public static IEnumerable<KeyValuePair<int, string>> Map(Person p)
        {
            if (p.Age > 22)
                yield return new KeyValuePair<int, string>(p.Age, p.Name);
        }
     */
    /// <summary>
    /// 一个Map/Reduce作业（job）通常会把输入的数据集切分成若干的数据块，由map任务（task）以完全并行的方式处理它们。
    /// 框架会对map的输出进行排序，然后结果输入给reduce任务。
    /// 通常作业的输入和输出都会被存储在文件系统中。
    /// 整个框架负责任务的调度和监控，以及重新执行已失败的任务。
    /// </summary>
    public static class MapReduceDemo
    {
        public static Dictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(
            this IEnumerable<TInput> list,
            Func<TInput, IEnumerable<KeyValuePair<TKey, TValue>>> map,
            Func<TKey, IEnumerable<TValue>, TResult> reduce)
        {
            Dictionary<TKey, List<TValue>> mapResult = new Dictionary<TKey, List<TValue>>();
            foreach (var item in list)
            {
                foreach (var one in map(item))
                {
                    List<TValue> mapValues;
                    if (!mapResult.TryGetValue(one.Key, out mapValues))
                    {
                        mapValues = new List<TValue>();
                        mapResult.Add(one.Key, mapValues);
                    }
                    mapValues.Add(one.Value);
                }
            }
            var result = new Dictionary<TKey, TResult>();
            foreach (var m in mapResult)
            {
                result.Add(m.Key, reduce(m.Key, m.Value));
            }
            return result;
        }
    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
